package com.example.service;

import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import com.example.domain.Result;
import com.example.domain.MsgEmail;
import com.example.util.CodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class MsgServiceImpl{
    @Autowired
    SendMailImpl sender;

    @Autowired
    private CodeUtil codeUtil;

    @CreateCache(name = "jetCache_", expire = 600)
    private Cache<String, String> jetCache;

    public Result sendCode(String email) {
        String s = codeUtil.generateCode(email);
        jetCache.put(email, s);
        sender.sendMail(email,s);
        return new Result(true);
    }

    public Result checkCode(MsgEmail msgEmail) {
        String code = msgEmail.getCode();
        String cacheCode = jetCache.get(msgEmail.getEmail());
        return new Result(code.equals(cacheCode),"验证码错误！");
    }

}
